package com.hyperion.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyperion.blog.utils.JWTUtils;
import com.hyperion.blog.dao.mapper.SysUserMapper;
import com.hyperion.blog.dao.pojo.SysUser;
import com.hyperion.blog.service.SysUserService;
import com.hyperion.blog.vo.LoginUserVo;
import com.hyperion.blog.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Hyperion
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 通过 authorId 找作者名
     *
     * @param authorId 文章名
     * @return 作者名
     */
    @Override
    public SysUser findAuthorByAuthorId(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setNickname("没找到用户！");
        }
        return sysUser;
    }

    /**
     * 通过账号密码查找账号
     *
     * @param account  账号
     * @param password 密码
     * @return 用户信息
     */
    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.eq(SysUser::getPassword, password);
        queryWrapper.select(SysUser::getAccount, SysUser::getId, SysUser::getAvatar, SysUser::getNickname);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    /**
     * 通过账号查找账号
     *
     * @param account 账号
     * @return 用户信息
     */
    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    /**
     * 通过 authorId 找作者信息
     *
     * @param authorId 作者名
     * @return 作者名
     */
    @Override
    public UserVo findUserVoById(Long authorId) {
        SysUser toUser = findAuthorByAuthorId(authorId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(toUser, userVo);
        return userVo;
    }

    /**
     * 通过 token 查找账号
     *
     * @param token 用户加密信息
     * @return 用户信息
     */
    @Override
    public LoginUserVo getUserInfoByToken(String token) {
        Map<String, Object> map = JWTUtils.checkToken(token);
        if (map == null) {
            return null;
        }
        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
        if (StringUtils.isBlank(userJson)) {
            return null;
        }
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setNickname(sysUser.getNickname());
        return loginUserVo;
    }

    /**
     * 注册用户，往数据库增加信息
     *
     * @param sysUser 用户数据
     */
    @Override
    public void saveUser(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

}
